<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAddPersonnel">新增店长</el-button>

    <el-table :data="personnelList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="id" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="登录名" width="180">
        <template slot-scope="scope">
          {{ scope.row.username }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="姓名" width="100">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="入职时间" width="220">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="注册人" width="120">
        <template slot-scope="scope">
          {{ scope.row.registerName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态" width="120">
        <template slot-scope="scope">
          {{ scope.row.adminStatus }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页查询 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
      :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="dataTotal" v-show="dataTotal>0">
    </el-pagination>
    <!-- 弹框 -->
    <PersonnelDialog :dialogType="dialogType" :dialogVisible.sync="dialogVisible" :personnel="personnel" :role="'Admin'"
      @dialogDataAdd="addPersonnelList" @dialogDataChange="changePersonnelList"></PersonnelDialog>
  </div>
</template>

<script>
  import {
    MessageBox,
    Message
  } from 'element-ui'
  import {
    getPersonnel,
    addPersonnel,
    updatePersonnel,
    deletePersonnel,
    getPersonnelByPage
  } from '@/api/personnel'
  import {
    deepClone
  } from '@/utils'
  import PersonnelDialog from './components/personnel_dialog.vue'

  export default {
    components: {
      PersonnelDialog
    },
    data() {
      return {
        personnelList: [],
        personnel: {

        },
        adminStatus: [{
            value: '冻结',
            label: '冻结'
          },
          {
            value: '正常',
            label: '正常'
          }
        ],
        dialogVisible: false,
        dialogType: 'new',
        // 分页
        currentPage: 1,
        pageSize:10,
        pageSizes:[10,15,20,100],
        dataTotal:0
      }
    },
    computed: {},
    created() {
      this.getPersonnel()
    },
    methods: {
      // 获取数据库员工信息
      async getPersonnel() {
        const {data} = await getPersonnelByPage("Admin",this.currentPage,this.pageSize)
        this.dataTotal = data.total
        this.personnelList = data.data
      },
      // 添加员工
      handleAddPersonnel() {
        this.dialogType = 'new'
        this.dialogVisible = true
        this.personnel = {}
      },
      // 修改员工
      handleEdit(scope) {
        this.personnel = deepClone(scope.row)
        this.dialogType = 'edit'
        this.dialogVisible = true
      },
      // 删除员工
      handleDelete({
        $index,
        row
      }) {
        this.$confirm('确定删除?', 'Warning', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.$prompt('请输入密钥', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              inputType: 'password'
            }).then(async ({
              value
            }) => {
              const obj = {
                id: row.id,
                secretKey: value
              }
              const res = await deletePersonnel(obj)
              if (res.code === 1) {
                this.personnelList.splice($index, 1)
                this.$message({
                  type: 'success',
                  message: res.msg
                })
              }
            })
          })
          .catch(err => {
            console.error(err)
          })
      },
      // 接收子组件传入的personnel
      addPersonnelList(data) {
        this.personnelList.push(data)
      },
      changePersonnelList(id, data) {
        for (let index = 0; index < this.personnelList.length; index++) {
          if (this.personnelList[index].id === data.id) {
            this.personnelList.splice(index, 1, Object.assign({}, data))
            break
          }
        }
      },
      // 分页功能
      handleSizeChange(val) {
        this.pageSize = val
        this.getPersonnel()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        this.getPersonnel()
      }
      //
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }

    .permission-tree {
      margin-bottom: 30px;
    }
  }
</style>
